package edu.stu.mall.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import edu.stu.mall.databinding.ItemProductCategoryBinding
import edu.stu.mall.repository.data.ProductCategoryListDataItem

class ProductCategoryAdapter : RecyclerView.Adapter<ProductCategoryAdapter.ProductCategoryViewHolder>() {

    private var dataList: List<ProductCategoryListDataItem> = mutableListOf()
    fun setData(list: List<ProductCategoryListDataItem>?) {
        if (list != null && list.isNotEmpty()) {
            dataList = list
            notifyDataSetChanged()
        }
    }


    class ProductCategoryViewHolder(binding: ItemProductCategoryBinding) : RecyclerView.ViewHolder(binding.root) {
        var itemBinding: ItemProductCategoryBinding

        init {
            itemBinding = binding
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProductCategoryViewHolder {

        return ProductCategoryViewHolder(
            ItemProductCategoryBinding.inflate(
                LayoutInflater.from(parent.context),
                parent, false
            )
        )
    }

    override fun getItemCount(): Int {
        return dataList.size

    }

    override fun onBindViewHolder(holder: ProductCategoryViewHolder, position: Int) {

        holder.itemBinding.item = dataList[position]
    }
}